package com.meteor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meteor.common.BlogDto;
import com.meteor.common.MarkdownUtils;
import com.meteor.mapper.BlogMapper;
import com.meteor.pojo.Blog;
import com.meteor.pojo.Type;
import com.meteor.service.BlogService;
import com.meteor.service.TypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogService {

    @Resource
    private TypeService typeService;

    @Resource
    private BlogService blogService;

    public void saveWithTitle(BlogDto blogDto) {
        //根据分类名称 查出分类对应id
        String type = blogDto.getType();
        //条件构造器
        LambdaQueryWrapper<Type> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件等值查询
        queryWrapper.eq(Type::getType,type);
        Type ty = typeService.getOne(queryWrapper);
        Blog blog = new Blog();
        //复制一下
        BeanUtils.copyProperties(blogDto,blog);
        blog.setTypeId(ty.getId());
        blogService.save(blog);
    }

    @Override
    public Blog getAndConvert(Long id) {
        Blog blog = blogService.getById(id);
        Blog b = new Blog();
        BeanUtils.copyProperties(blog, b);
        String content = b.getContent();
        String s = MarkdownUtils.markdownToHtmlExtensions(content);
        b.setContent(s);
        return b;
    }
}
